package com.example.springboothomework.controller;


import com.example.springboothomework.common.BaseResponse;
import com.example.springboothomework.common.PageResponse;
import com.example.springboothomework.common.ResultUtils;
import com.example.springboothomework.entity.dto.CompanyDTO;
import com.example.springboothomework.entity.Company;
import com.example.springboothomework.entity.query.CompanyQuery;
import com.example.springboothomework.entity.vo.CompanyInfoVo;
import com.example.springboothomework.service.ICompanyService;
import com.example.springboothomework.validator.ValidGroup;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author: chenjianhao
 * @date: 2025/6/5
 * @description: 公司控制器
 */
@Slf4j
@RestController
@RequestMapping("/company")
@RequiredArgsConstructor
@Validated
public class CompanyController {

    private final ICompanyService companyService;

    /**
     * 添加公司
     *
     * @param companyDTO  公司dto
     * @author: chenjianhao
     * @date: 2025/6/5
     * @return
     */
    @PostMapping("/save")
    public BaseResponse<Long> save(@Validated(ValidGroup.Save.class) @RequestBody CompanyDTO companyDTO) {
        log.info("添加公司：addCompanyDto:{}", companyDTO);
        Long companyId = companyService.save(companyDTO);
        return ResultUtils.success(companyId);
    }

    /**
     * 删除公司
     *
     * @param id  id
     * @author: chenjianhao
     * @date: 2025/6/5
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public BaseResponse<Void> delete(@NotNull @Min(1L) @PathVariable Long id) {
        log.info("删除公司：id:{}", id);
        companyService.delete(id);
        return ResultUtils.success();
    }

    /**
     * 更新公司
     *
     * @param companyDTO  公司dto
     * @author: chenjianhao
     * @date: 2025/6/5
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Void> update(@Validated(ValidGroup.Update.class) @RequestBody CompanyDTO companyDTO) {
        log.info("更新公司：company:{}", companyDTO);
        companyService.update(companyDTO);
        return ResultUtils.success();
    }

    /**
     * 通过id查询详情
     *
     * @param id  id
     * @author: chenjianhao
     * @date: 2025/6/5
     * @return
     */
    @GetMapping("/info/{id}")
    public BaseResponse<Company> info(@NotNull @Min(1L) @PathVariable Long id) {
        log.info("获取公司：id:{}", id);
        return ResultUtils.success(companyService.info(id));
    }

    /**
     * 分页查询公司列表
     *
     * @param companyQuery  公司查询参数
     * @author: chenjianhao
     * @date: 2025/6/5
     * @return
     */
    @PostMapping("/query-page")
    public BaseResponse<PageResponse<Company>> queryPage(@Valid @RequestBody CompanyQuery companyQuery) {
        Integer pageNum = companyQuery.getPageNum();
        Integer pageSize = companyQuery.getPageSize();
        String searchKeyword = companyQuery.getSearchKeyword();
        log.info("获取公司列表: page={}, size={}, keyword={}", pageNum, pageSize, searchKeyword);
        return ResultUtils.success(companyService.queryPage(pageNum, pageSize, searchKeyword));
    }

    /**
     * 获取所有公司的id和name
     *
     * @author: chenjianhao
     * @date: 2025/6/5
     * @return
     */
    @GetMapping("/get-all-id-and-name")
    public BaseResponse<List<CompanyInfoVo>> getAllIdAndName() {
        log.info("获取所有公司的id和name");
        return ResultUtils.success(companyService.getAllIdAndName());
    }


}
